//
//  DHStatusCellTopView.swift
//  Weibo
//
//  Created by Holfioo on 15/9/7.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit


class DHStatusCellTopView: UIView {
    // 微博视图模型
    var statusViewModel: DHStatusViewModel? {
        didSet {
            // TODO: 布局界面
            nameLab.text = statusViewModel?.status.user!.name
            // 设置头像。在视图模型中做计算型属性
            iconView.sd_setImageWithURL(statusViewModel?.userIconUrl)
            // 设置 vip 图标
            vipView.image = statusViewModel?.userVipImage
            memberView.image = statusViewModel?.userMember
            
            //: TODO: timeLab 和 source
        }
    }
    // 重写初始化方法
    override init(frame: CGRect) {
        // 布局UI
        super.init(frame: frame) // 父类初始化
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        addSubview(iconView)
        addSubview(nameLab)
        addSubview(vipView)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        
        // 布局约束
        let offsetX = CGPoint(x: DHStatusCellMargin, y: 0)
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: DHStatusIconWH, height: DHStatusIconWH), offset: CGPoint(x: DHStatusCellMargin, y: DHStatusCellMargin))
        nameLab.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: offsetX)
        memberView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLab, size: nil, offset: offsetX)
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: offsetX)
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: offsetX)
        vipView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 8))
    }
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLab: UILabel = UILabel(title:"姓名", color: UIColor.darkGrayColor(), fontSize: 15)
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), fontSize: 10)
    private lazy var sourceLabel: UILabel = UILabel(title: "来自 新浪微博", color: UIColor.darkGrayColor(), fontSize: 10)
}
